package eva.savant.business.ventas.view

import eva.savant.businessobject.vt.Venta
import eva.savant.businessobject.vt.Vendedor
import eva.savant.businessobject.vt.VentaDetalle

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.convert.Converter

@SessionScoped
@ManagedBean(name = 'IIVentasVendedor')
public class IIVentasVendedor extends Bean {

  Date dateIni, dateEnd

  @PostConstruct
  public void cleanEntity() {

    lsEntity = null

    entity = new Venta()

    dateIni= dateEnd= new Date()
  }

  public List findLsEntity() {

    dateIni= toStartDay(dateIni); dateEnd= toMidnight(dateEnd); form= true


    if(entity.vendedor?.id != null) {

    def d= get(VentaDetalle.class,'sum(importe), sum(descuento), sum(total): venta.operacionId > 0 and venta.vendedor.id=? and venta.fecha between ? and ?', entity.vendedor.id, dateIni, dateEnd)

    entity.importe= d[0]; entity.descuento= d[1]; entity.total= d[2]

    return find(VentaDetalle.class,'material, sum(cantidad), precio, sum(importe), sum(descuento), sum(total): ' +

                                   'venta.operacionId > 0 and venta.vendedor.id=? and venta.fecha between ? and ? group by material.id order by material.id', entity.vendedor.id, dateIni, dateEnd)
    }


    def d= get(VentaDetalle.class,'sum(importe), sum(descuento), sum(total): venta.operacionId > 0 and venta.fecha between ? and ?', dateIni, dateEnd)

    entity.importe= d[0]; entity.descuento= d[1]; entity.total= d[2]


    return find(VentaDetalle.class,'material, sum(cantidad), precio, sum(importe), sum(descuento), sum(total): ' +

                                   'venta.operacionId > 0 and venta.fecha between ? and ? group by material.id order by material.id', dateIni, dateEnd)

    //def sql= new Sql(toDataBase()); sql.eachRow('delc') { println 'd: '+it.nombre }
  }

  public List complete(String value) { return completeLike(Vendedor.class,'persona.nombre',value) }

  public Converter getConverter() { return converterLike(Vendedor.class) }

}
